<?php

namespace Hilaoyu\LaravelExtend\Services\Files\Controllers;

use Hilaoyu\LaravelExtend\ControllerBase;
use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Hilaoyu\LaravelExtend\Supports\Uuid;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Http\Request;


class FilesController extends ControllerBase
{

    use FilesControllerHelperTrait;

    public function __construct()
    {
        UtilSupport::tryQuiet(function () {
            parent::__construct();
        });

    }


    public function uploader(Request $request, $disk)
    {
        return $this->checkAllowUpload($request, $disk)
            ->then(function ($request) use ($disk) {
                $uoploadController = new FileUploadController();
                return $uoploadController->uploader($request, $disk);

            });
    }

    public function uploadTaskCreate(Request $request, $disk)
    {
        return $this->checkAllowUpload($request, $disk)
            ->then(function ($request) use ($disk) {
                $uoploadController = new FileUploadController();
                return $uoploadController->uploadTaskCreate($request, $disk);

            });
    }

    public function uploadTaskGet(Request $request)
    {
        $id = $request->input('id', '');
        if (!$id || !$filesModel = FileService::loadFilesModel($id)) {
            abort(404);
        }
        $disk = $filesModel->getAttribute('disk');
        return $this->checkAllowUpload($request, $disk)
            ->then(function ($request) use ($filesModel) {
                $uoploadController = new FileUploadController();
                return $uoploadController->uploadTaskGet($request, $filesModel);

            });
    }

    public function uploadTaskComplete(Request $request)
    {
        $id = $request->input('id', '');
        if (!$id || !$filesModel = FileService::loadFilesModel($id)) {
            abort(404);
        }
        $disk = $filesModel->getAttribute('disk');
        return $this->checkAllowUpload($request, $disk)
            ->then(function ($request) use ($filesModel) {
                $uoploadController = new FileUploadController();
                return $uoploadController->uploadTaskComplete($request, $filesModel);

            });
    }

    public function upload(Request $request, $disk)
    {


        return $this->checkAllowUpload($request, $disk)
            ->then(function ($request) use ($disk) {

                $task_id = $request->input('task_id', '');
                if ($task_id) {
                    if (!$filesModel = FileService::loadFilesModel($task_id)) {
                        return comResponse()->failed('任务错误')
                            ->json();
                    }

                    if ($disk != $filesModel->getAttribute('disk')) {
                        return comResponse()->failed('任务错误!')
                            ->json();
                    }
                } else {
                    if (!$filesModel = FileService::newFilesModel($disk)) {
                        return comResponse()->failed('创建任务出错')
                            ->json();
                    }
                }

                $uoploadController = new FileUploadController();
                return $uoploadController->upload($request, $filesModel);

            });
    }


    public function view(Request $request, $disk, $path)
    {
        return $this->checkAllowView($request, $disk, $path)
            ->then(function ($request) use ($disk, $path) {
                $uoploadController = new FileViewController();
                return $uoploadController->view($request, $disk, $path);

            });
    }

    public function file(Request $request, $id)
    {
        if (!$filesModel = FileService::loadFilesModel($id)) {
            abort(404);
        }

        $disk = $filesModel->getAttribute('disk');
        return $this->checkAllowView($request, $disk)
            ->then(function ($request) use ($filesModel) {
                $uoploadController = new FileViewController();
                return $uoploadController->file($request, $filesModel);

            });
    }

    public function fileInfo(Request $request)
    {

        $url = $request->input('url', '');
        if(Uuid::verify($url)){
            $filesModel = FileService::loadFilesModel($url);
            $FS = new FileService("","",$filesModel);
        }else{
            $FS = FileService::make($url);
            $filesModel = $FS->filesModel();
        }

        $disk = $FS->filesModel('disk');
        $id = $FS->filesModel('id');
        $path = $FS->filesModel('path');
        if ($disk && $client = FileService::useAsClient($disk)) {
            $filesModel_tmp = $client->fileInfo([
                'id' => $id,
                'path' => $path,
            ]);
            //dd($filesModel_tmp,$client->getResult('message'),$client->getError());

            if ($filesModel_tmp) {
                if ($filesModel->getKey()) {
                    $filesModel->fill($filesModel_tmp);
                    if ($filesModel->getAttribute('is_permanent')) {
                        $filesModel->save();
                    } else {
                        $filesModel->saveToCache();
                    }
                }
                $filesModel = $filesModel_tmp;
            }
        }


        return comResponse()->success()->with($filesModel)->json();
    }

}
